package com.sdr.sluicemapplugin.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.sdr.xitang.R;
import com.sdr.sluicemapplugin.activities.ZhaZhanController;
import com.sdr.sluicemapplugin.activities.ZhaZhanFenBu;
import com.sdr.sluicemapplugin.activities.ZhaZhanZhuangTai;
import com.sdr.sluicemapplugin.bean.WaterGateBean;

import java.util.List;

/**
 * Created by HeYongFeng on 2016/10/14.
 */
public class GatelistDialogAdapter extends BaseAdapter{

    private Activity context;
    List<WaterGateBean.GateListBean> gateList;
    private String userId;
    private String accessToken;
    private double latitude;
    private double longitude;
    private String serverUri;
    private int videoCount;
    private String autoType;

    public GatelistDialogAdapter(Activity context, List<WaterGateBean.GateListBean> gateList, String userId, String accessToken, double latitude, double longitude,String serverUri,int videoCount,String autoType) {
        this.context = context;
        this.gateList = gateList;
        this.userId = userId;
        this.accessToken = accessToken;
        this.latitude = latitude;
        this.longitude = longitude;
        this.serverUri = serverUri;
        this.videoCount = videoCount;
        this.autoType = autoType;
    }

    @Override
    public int getCount() {
        return gateList.size();
    }

    @Override
    public Object getItem(int position) {
        return gateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.customer_dialog_listview_item,null);
        TextView tv_dialog_lv_item_name = (TextView) convertView.findViewById(R.id.tv_dialog_lv_item_name);
        TextView tv_dialog_lv_item_status = (TextView) convertView.findViewById(R.id.tv_dialog_lv_item_status);
        //显示是否可以控制
        TextView tv_isCtrl = (TextView) convertView.findViewById(R.id.tv_isCtrl);
        if (isControl(gateList.get(position).getName()) && "2".equals(autoType)){
            tv_isCtrl.setVisibility(View.VISIBLE);
        }
        tv_dialog_lv_item_name.setText(gateList.get(position).getName());
        //设置开启可见
        tv_dialog_lv_item_status.setVisibility(View.VISIBLE);
        if (gateList.get(position).getStatus() == 0){
            tv_dialog_lv_item_status.setText("(关闭)");
            tv_dialog_lv_item_status.setTextColor(Color.parseColor("#737373"));
        }else if (gateList.get(position).getStatus() == 1){
            tv_dialog_lv_item_status.setText("(开启)");
            tv_dialog_lv_item_status.setTextColor(Color.parseColor("#33cd5f"));
        }else if (gateList.get(position).getStatus() == 2){
            tv_dialog_lv_item_status.setText("(故障)");
            tv_dialog_lv_item_status.setTextColor(Color.parseColor("#ef473a"));
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isControl(gateList.get(position).getName()) && "2".equals(autoType)){
                    context.startActivity(new Intent(context,ZhaZhanController.class));
                }else {
                    Intent intent = new Intent(context, ZhaZhanZhuangTai.class);
                    intent.putExtra("userId", userId);
                    intent.putExtra("accessToken", accessToken);
                    intent.putExtra("SGPCD", gateList.get(position).getSgpcd());
                    intent.putExtra("latitude", latitude);
                    intent.putExtra("longitude", longitude);
                    intent.putExtra("name", gateList.get(position).getName());
                    intent.putExtra("serverUri", serverUri);
                    intent.putExtra("videoCount", videoCount);
                    context.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    private boolean isControl(String name){
        if (name.contains("闸")){
        return true;
        }else {
            return false;
        }
    }

//    private void showCustomDialog() {
//        //让之前的Dialog消失
//        if (((ZhaZhanFenBu)context).custom_gatelist_dialog.isShowing()) {
//            ((ZhaZhanFenBu) context).custom_gatelist_dialog.dismiss();
//        }
//        custom_dialog_select = new Dialog(context, R.style.GatelistDialog);
//        View view = View.inflate(context,R.layout.customer_dialog_showgatelist,null);
//        ListView lv_gateListView = (ListView) view.findViewById(R.id.lv_gateListView);
//        TextView tv_custom_dialog_title = (TextView) view.findViewById(R.id.tv_custom_dialog_title);
//        tv_custom_dialog_title.setText("选项");
//        lv_gateListView.setAdapter(new ArrayAdapter<String>(context,R.layout.customer_dialog_listview_item,R.id.tv_dialog_lv_item_name,new String[]{"上传闸站状态","闸站远程控制"}));
//        lv_gateListView.setOnItemClickListener(this);
//
//        custom_dialog_select.setContentView(view);
//        custom_dialog_select.setCancelable(true);
//        custom_dialog_select.show();
//        ((ZhaZhanFenBu)context).setDialogWidthHeight(custom_dialog_select);
//    }

//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        switch (position){
//            //点击的是上传闸站的信息
//            case 0:
//                Intent intent = new Intent(context, ZhaZhanZhuangTai.class);
//                intent.putExtra("userId", userId);
//                intent.putExtra("accessToken", accessToken);
//                intent.putExtra("SGPCD", gateList.get(clickPosition).getSgpcd());
//                intent.putExtra("latitude", latitude);
//                intent.putExtra("longitude", longitude);
//                intent.putExtra("name", gateList.get(clickPosition).getName());
//                intent.putExtra("serverUri", serverUri);
//                intent.putExtra("videoCount", videoCount);
//                context.startActivity(intent);
//            break;
//            //点击的是控制闸站的按钮
//            case 1:
//                context.startActivity(new Intent(context,ZhaZhanController.class));
//            break;
//        }
//        Toast.makeText(context, "您点击了："+(position+1), Toast.LENGTH_SHORT).show();
//        if (custom_dialog_select.isShowing()){
//            custom_dialog_select.dismiss();
//        }
//    }
}
